package com.qiyigu.itc.speech.baidu;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;


import com.qiyigu.itc.speech.MyApp;
import com.qiyigu.itc.speech.SpeechDialogView;
import com.qiyigu.itc.speech.baidu.control.MyWakeup;
import com.qiyigu.itc.speech.baidu.recognization.IStatus;
import com.qiyigu.itc.speech.baidu.wakeup.IWakeupListener;
import com.qiyigu.itc.speech.baidu.wakeup.RecogWakeupListener;
import com.qiyigu.itc.speech.baidu.wakeup.WakeupParams;
import com.qiyigu.itc.speech.service.BaseServiceImpl;
import com.qiyigu.itc.speech.service.WarkUpService;
import com.qiyigu.itc.speech.service.listener.OnSdkWarkUpListener;
import com.qiyigu.itc.speech.service.listener.OnSpeechListener;

import java.util.Map;

import static com.qiyigu.itc.speech.baidu.recognization.IStatus.STATUS_NONE;

public class BaiduWarkupServiceImpl extends BaseServiceImpl implements WarkUpService, IStatus {

    protected static BaiduWarkupServiceImpl service = null;
    /**
     * 0: 方案1， 唤醒词说完后，直接接句子，中间没有停顿。
     * >0 : 方案2： 唤醒词说完后，中间有停顿，然后接句子。推荐4个字 1500ms
     * <p>
     * backTrackInMs 最大 15000，即15s
     */
    private int backTrackInMs = 2000;

    protected MyWakeup myWakeup;
    private int status = STATUS_NONE;

    protected OnSdkWarkUpListener onSdkWarkUpListener;
    protected OnSpeechListener onSpeechListener;

    protected BaiduWarkupServiceImpl(OnSdkWarkUpListener onSdkWarkUpListener, OnSpeechListener onSpeechListener) {
        service = this;
        this.onSdkWarkUpListener = onSdkWarkUpListener;
        this.onSpeechListener = onSpeechListener;
        IWakeupListener listener = new RecogWakeupListener(mHandler);
        myWakeup = new MyWakeup(MyApp.getInstance(), listener);
    }

    public static synchronized BaiduWarkupServiceImpl getInstance(OnSdkWarkUpListener
                                                                            onSdkWarkUpListener, OnSpeechListener onSpeechListener) {
        if (service == null) {
            service = new BaiduWarkupServiceImpl(onSdkWarkUpListener,onSpeechListener);
        }
        return service;
    }

    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == STATUS_WAKEUP_SUCCESS) {
                logger.i("唤醒成功");
                if (msg.obj != null){
                    SpeechDialogView.getInstance(MyApp.getInstance()).setTextSpeech(msg.obj.toString());
                }
                if (onSpeechListener != null){
                    onSpeechListener.onWarkUp();
                }
                if (onSdkWarkUpListener != null) {
                    onSdkWarkUpListener.onWarkUpReady();
                }
            }

        }
    };

    @Override
    public synchronized void startWarkUp() {
        if (status != STATUS_NONE) {
            return;
        }
        WakeupParams wakeupParams = new WakeupParams(MyApp.getInstance());
        Map<String, Object> params = wakeupParams.fetch();
        myWakeup.start(params);
        status = STATUS_WAITING_READY;
        logger.i("启动唤醒");
    }

    @Override
    public synchronized void stopWarkUp() {
        myWakeup.stop();
        status = STATUS_NONE;
    }

    @Override
    public synchronized void releaseWarkUp() {
        myWakeup.release();
        status = STATUS_NONE;
    }
}
